<!--  -->
<template>
  <el-dialog
    v-model="showSku"
    title="编辑货品信息"
    top="30vh"
    :before-close="() => cancel()"
  >
    <div class="demo-input-size">
      <span class="ipt-title">商品货号:</span>
      <span class="ipt-val">
        <el-input
          v-model="iptVal"
          class="w-50 m-2"
          size="large"
          placeholder="按sku编号搜索"
        />
      </span>
      <span> <el-button :icon="Search" size="large" @click="searchFn" /></span>
    </div>

    <el-form>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column align="center" label="SKU编号" width="300" />
        <el-table-column align="center" label="销售价格" />
        <el-table-column align="center" label="商品库存" />
        <el-table-column align="center" label="库存预警值" />
      </el-table>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel" size="large">取消</el-button>
        <el-button type="primary" @click="" size="large">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, toRefs, ref, Ref } from "vue";
import * as http from "../../../request/http";
import { Search } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";

const tableData = ref([]) as Ref<getSkuDataRes[]>;
let props = defineProps<{
  showSku: boolean;
  pid: number;
}>();

// 获取页面数据
let keyword = ref("");

async function SkuFetchData() {
  let res = await http.getSkuData(props.pid, {
    keyword: keyword.value,
  });
  // console.log(res);

  if (res.code === 200) {
    // console.log(res.message);
    tableData.value = res.data;
  }
}
SkuFetchData();

// 点击取消
let emit = defineEmits<{
  (event: "hidden"): void;
}>();

function cancel() {
  emit("hidden");
}

// 点击搜索
const iptVal = ref("");
// toast提示框

type type = "info" | "success" | "warning" | "error";
let messages = ref("");
let types = ref("info") as Ref<type>;
const openToast = (message: string, type: type) => {
  ElMessage({
    message: message,
    type: type,
  });
};
async function searchFn() {
  if (iptVal.value === "") {
    messages.value = "编号不能为空";
    types.value = "warning";
    openToast(messages.value, types.value);
  } else {
    let res = await http.getSkuData(props.pid, {
      keyword: iptVal.value,
    });
    if (res.code === 200) {
      // console.log(res.message);
      tableData.value = res.data;
      messages.value = res.message;
      types.value = "success";
      openToast(messages.value, types.value);
    } else {
      messages.value = res.message;
      types.value = "error";
      openToast(messages.value, types.value);
    }
  }
}
</script>
<style lang="less" scoped>
.demo-input-size {
  margin-bottom: 20px;
  // width: 100px;
  display: flex;
  align-items: center;
  .ipt-title {
    width: 80px;
  }
  .ipt-val {
    width: 300px;
  }
}
</style>
